package com.sss.hloster01;

public class Producer extends Thread {

	// 属性
	// 仓库
	private WarehouseStack whStack;
	// 多线程终止采用外部传入停止参数
	private boolean isStopped;

	// 行为
	public void run() {
		int num = 0;
		// 生产者负责生产产品
		while (true) {
			synchronized (whStack) {
				// 判定仓库栈是否为满
				// 如果仓库为满，则当前等待消费者者消费产品
				if (whStack.getSize() == whStack.getCapacity()) {
					try {
						System.out.println("仓库满，生产者进入仓库的等待状态！！直到消费者将其唤醒");
						whStack.wait();
					} catch (InterruptedException e) {

						e.printStackTrace();
					}
				}
				Product pro = new Product(" " + num++, Math.random() * 10 + 90);
				// 将产品放入栈中
				whStack.push(pro);
				// 输出信息
				System.out.println("生产者生产产品：：" + pro.printInfo());
				whStack.notifyAll();
			}
			// 休眠
			try {
				Thread.sleep(150);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public Producer(WarehouseStack whStack) {
		super();
		this.whStack = whStack;
	}
}
